package com.draven.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.draven.common.entity.NoteCategory;
import com.draven.system.mapper.NoteCategoryMapper;
import com.draven.system.service.INoteCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 笔记分类Service业务层处理
 *
 * @author draven
 */
@Service
public class NoteCategoryServiceImpl extends ServiceImpl<NoteCategoryMapper, NoteCategory> implements INoteCategoryService {

    @Autowired
    private NoteCategoryMapper noteCategoryMapper;

    /**
     * 查询分类列表
     *
     * @param category 分类信息
     * @return 分类列表
     */
    @Override
    public List<NoteCategory> selectCategoryList(NoteCategory category) {
        LambdaQueryWrapper<NoteCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(category.getCategoryName()), NoteCategory::getCategoryName, category.getCategoryName());
        queryWrapper.eq(category.getStatus() != null, NoteCategory::getStatus, category.getStatus());
        queryWrapper.orderByAsc(NoteCategory::getSortOrder);
        queryWrapper.orderByDesc(NoteCategory::getCreateTime);
        return noteCategoryMapper.selectList(queryWrapper);
    }

    /**
     * 查询分类列表（包含笔记数量）
     *
     * @return 分类列表
     */
    @Override
    @Cacheable(value = "categoryListWithCount", key = "#root.methodName", unless = "#result == null || #result.size() == 0")
    public List<NoteCategory> selectCategoryListWithCount() {
        return noteCategoryMapper.selectCategoryListWithCount();
    }

    /**
     * 根据分类ID查询分类信息
     *
     * @param categoryId 分类ID
     * @return 分类信息
     */
    @Override
    public NoteCategory selectCategoryById(Long categoryId) {
        return noteCategoryMapper.selectById(categoryId);
    }

    /**
     * 校验分类名称是否唯一
     *
     * @param category 分类信息
     * @return 结果
     */
    @Override
    public boolean checkCategoryNameUnique(NoteCategory category) {
        Long categoryId = category.getId() == null ? -1L : category.getId();
        NoteCategory info = noteCategoryMapper.selectByCategoryName(category.getCategoryName());
        if (info != null && info.getId().longValue() != categoryId.longValue()) {
            return false;
        }
        return true;
    }

    /**
     * 新增分类
     *
     * @param category 分类信息
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryListWithCount", key = "'selectCategoryListWithCount'")
    public int insertCategory(NoteCategory category) {
        return noteCategoryMapper.insert(category);
    }

    /**
     * 修改分类
     *
     * @param category 分类信息
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryListWithCount", key = "'selectCategoryListWithCount'")
    public int updateCategory(NoteCategory category) {
        return noteCategoryMapper.updateById(category);
    }

    /**
     * 批量删除分类
     *
     * @param categoryIds 需要删除的分类ID
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryListWithCount", key = "'selectCategoryListWithCount'")
    public int deleteCategoryByIds(Long[] categoryIds) {
        return noteCategoryMapper.deleteBatchIds(Arrays.asList(categoryIds));
    }

    /**
     * 删除分类信息
     *
     * @param categoryId 分类ID
     * @return 结果
     */
    @Override
    @CacheEvict(value = "categoryListWithCount", key = "'selectCategoryListWithCount'")
    public int deleteCategoryById(Long categoryId) {
        return noteCategoryMapper.deleteById(categoryId);
    }

    @Override
    public NoteCategory selectById(Long id) {
        return noteCategoryMapper.selectById(id);
    }

    @Override
    public List<NoteCategory> listAll() {
        return noteCategoryMapper.selectList(null);
    }
}
